extern crate fejpsl_contract;
use fejpsl_contract::article::Article;

pub async fn top_list_article() -> Option<Vec<Article>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_article = Vec::new();
    let rows = &client.query("SELECT id, apk_link, audit, author, chapter_id, chapter_name, item_collect, course_id, item_desc, \
    envelope_pic, fresh, item_link, nice_date, nice_share_date, origin, prefix, project_link, publish_time, share_date, share_user, super_chapter_id, \
    super_chapter_name, title, item_type, user_id, visible, zan, rank  FROM t_article limit 5"
                             , &[]).await.unwrap();
    for row in rows {
        let mut m_apk_link=String::from("");
        let tmp_str: Option<String> = row.get(1);
        if let Some(_i) = tmp_str {m_apk_link= row.get(1); }else{ m_apk_link=String::from("") }
        let mut m_audit=0;
        let tmp_str: Option<i32> = row.get(2);
        if let Some(_i) = tmp_str {m_audit= row.get(2); }else{ m_audit=0 }
        let mut m_author=String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str {m_author= row.get(3); }else{ m_author=String::from("") }
        let mut m_chapter_id=0;
        let tmp_str: Option<i32> = row.get(4);
        if let Some(_i) = tmp_str {m_chapter_id= row.get(4); }else{ m_chapter_id=0 }
        let mut m_chapter_name=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_chapter_name= row.get(5); }else{ m_chapter_name=String::from("") }
        let mut m_item_desc=String::from("");
        let tmp_str: Option<String> = row.get(8);
        if let Some(_i) = tmp_str {m_item_desc= row.get(8); }else{ m_item_desc=String::from("") }
        let mut m_nice_date=String::from("");
        let tmp_str: Option<String> = row.get(12);
        if let Some(_i) = tmp_str {m_nice_date= row.get(12); }else{ m_nice_date=String::from("") }
        let mut m_super_chapter_name=String::from("");
        let tmp_str: Option<String> = row.get(21);
        if let Some(_i) = tmp_str {m_super_chapter_name= row.get(21); }else{ m_super_chapter_name=String::from("") }

        let article = Article {
            id:   row.get(0),
            apk_link:m_apk_link,
            audit: m_audit,
            author:   m_author,
            chapter_id:m_chapter_id,
            chapter_name: m_chapter_name,
            item_collect: false,//row.get(6),
            course_id: 0,//row.get(7),
            item_desc:  m_item_desc,
            envelope_pic: row.get(9),
            fresh:  false,//row.get(10),
            item_link:  String::from(""), //row.get(11),
            nice_date: m_nice_date,//row.get(12),
            nice_share_date:  String::from(""),// row.get(13),
            origin:    String::from(""),//row.get(14),
            prefix:  String::from(""),//row.get(15),
            project_link:  String::from(""),// row.get(16),
            publish_time: 0,//row.get(17),
            share_date:  String::from(""),// row.get(18),
            share_user:  String::from(""),//  row.get(19),
            super_chapter_id: 0,//row.get(20),
            super_chapter_name:   m_super_chapter_name,//row.get(21),
            title:   row.get(22),
            item_type: 0,//row.get(23),
            user_id: 0,// row.get(24),
            visible: 0,//row.get(25),
            zan:  row.get(26),
            top:   String::from(""),// row.get(27),
        };
        vec_article.push(article);
    }
    return Some(vec_article);
}
pub async fn get_article_list_by_page_no(page_no:&String) -> Option<Vec<Article>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_article = Vec::new();
    let per_page_num:i64=5;
    let offset:i64;
    offset=(page_no.parse::<i64>().unwrap()+1)*per_page_num;
    let rows = &client.query("SELECT id, apk_link, audit, author, chapter_id, chapter_name, item_collect, course_id, item_desc, \
    envelope_pic, fresh, item_link, nice_date, nice_share_date, origin, prefix, project_link, publish_time, share_date, share_user, super_chapter_id, \
    super_chapter_name, title, item_type, user_id, visible, zan, rank  FROM t_article order by id limit $1 offset $2", &[&per_page_num,&offset]).await.unwrap();
    for row in rows {
        let mut m_apk_link=String::from("");
        let tmp_str: Option<String> = row.get(1);
        if let Some(_i) = tmp_str {m_apk_link= row.get(1); }else{ m_apk_link=String::from("") }
        let mut m_audit=0;
        let tmp_str: Option<i32> = row.get(2);
        if let Some(_i) = tmp_str {m_audit= row.get(2); }else{ m_audit=0 }
        let mut m_author=String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str {m_author= row.get(3); }else{ m_author=String::from("") }
        let mut m_chapter_id=0;
        let tmp_str: Option<i32> = row.get(4);
        if let Some(_i) = tmp_str {m_chapter_id= row.get(4); }else{ m_chapter_id=0 }
        let mut m_chapter_name=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_chapter_name= row.get(5); }else{ m_chapter_name=String::from("") }
        let mut m_item_desc=String::from("");
        let tmp_str: Option<String> = row.get(8);
        if let Some(_i) = tmp_str {m_item_desc= row.get(8); }else{ m_item_desc=String::from("") }
        let mut m_nice_date=String::from("");
        let tmp_str: Option<String> = row.get(12);
        if let Some(_i) = tmp_str {m_nice_date= row.get(12); }else{ m_nice_date=String::from("") }
        let mut m_super_chapter_name=String::from("");
        let tmp_str: Option<String> = row.get(21);
        if let Some(_i) = tmp_str {m_super_chapter_name= row.get(21); }else{ m_super_chapter_name=String::from("") }
        let article = Article {
            id:   row.get(0),
            apk_link:m_apk_link,
            audit: m_audit,
            author:   m_author,
            chapter_id:m_chapter_id,
            chapter_name: m_chapter_name,
            item_collect: false,//row.get(6),
            course_id: 0,//row.get(7),
            item_desc:  m_item_desc,
            envelope_pic: row.get(9),
            fresh:  false,//row.get(10),
            item_link:  String::from(""), //row.get(11),
            nice_date: m_nice_date,
            nice_share_date:  String::from(""),// row.get(13),
            origin:    String::from(""),//row.get(14),
            prefix:  String::from(""),//row.get(15),
            project_link:  String::from(""),// row.get(16),
            publish_time: 0,//row.get(17),
            share_date:  String::from(""),// row.get(18),
            share_user:  String::from(""),//  row.get(19),
            super_chapter_id: 0,//row.get(20),
            super_chapter_name:  m_super_chapter_name,//row.get(21),
            title:   row.get(22),
            item_type: 0,//row.get(23),
            user_id: 0,// row.get(24),
            visible: 0,//row.get(25),
            zan:  row.get(26),
            top:   String::from(""),// row.get(27),
        };

        vec_article.push(article);
    }
    return Some(vec_article);
}
